import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { FormBuilder } from '@angular/forms';
import { environment } from '@env/environment';
import { STChange, STClickRowClassNameType, STColumn, STComponent, STPage } from '@delon/abc/st';
import { CargoStorageService } from '../../cargo-storage.service';
import { deepCopy } from '@delon/util';

@Component({
  selector: 'app-detail',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.less']
})
export class CargoStoragePositionDetailComponent implements OnInit {
	@Input()
	storageInfo: any;

	selectedStorage: any;

	storagePageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	storagePageSetting: STPage = {
		total: '',
		show: true,
		showSize: true,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};

	tallyPageInfo = {
		total: 0,
		current: 1,
		size: 20,
		loading: false,
		nzPageSizeOptions: environment.nzPageSizeOptions
	};
	tallyPageSetting: STPage = {
		total: '',
		show: true,
		showSize: true,
		front: false,
		pageSizes: environment.nzPageSizeOptions
	};

	@ViewChild('st1', { static: false }) private storageSt!: STComponent;
	storageColumns: STColumn[] = [
		{
			title: '序号',
			width: 50,
			index: 'index',
			type: 'no',
			noIndex: (item, col, index) => index + 1 + (this.storageSt.pi - 1) * this.storageSt.ps,
			fixed: 'left',
			className: 'tc'
		},
		{
			title: '船名',
			index: 'shipNameZh',
			width: 80
		},
		{
			title: '航次',
			index: 'voyage',
			width: 80
		},
		{
			title: '货名',
			index: 'billCargoTypeName',
			width: 60
		},
		{
			title: '进出口',
			index: 'ieName',
			width: 50
		},
		{
			title: '单号',
			index: 'billNo',
			width: 100
		},
		{
			title: '货位',
			index: 'spot',
			width: 80
		},
		{
			title: '件吨',
			index: 'pieceTonInfo',
			width: 90
		},
		{
			title: '货主',
			index: 'cargoOwnerName',
			width: 90
		},
		{
			title: '货代',
			index: 'cargoAgentName',
			width: 90
		}
	];
	storageDataList = [];

	@ViewChild('st2', { static: false }) private tallySt!: STComponent;
	tallyColumns: STColumn[] = [
		{
			title: '序号',
			width: 50,
			index: 'index',
			type: 'no',
			noIndex: (item, col, index) => index + 1 + (this.tallySt.pi - 1) * this.tallySt.ps,
			fixed: 'left',
			className: 'tc'
		},
		{
			title: '日期班次',
			index: 'timeInfo',
			width: 100
		},
		{
			title: '航陆运',
			index: 'shipLandName',
			width: 80
		},
		{
			title: '作业过程',
			index: 'operationTypeName',
			width: 80
		},
		{
			title: '出入库',
			index: 'tallyTypeName',
			width: 80
		},
		{
			title: '理货件吨',
			index: 'tallyPieceTonInfo',
			width: 100
		},
		{
			title: '检算件吨',
			index: 'checkPieceTonInfo',
			width: 100
		},
		{
			title: '机械',
			index: 'machineInfo',
			width: 100
		},
		{
			title: '队组',
			index: 'workerInfo',
			width: 100
		},
		{
			title: '保管员',
			index: 'keeperName',
			width: 80
		},
		{
			title: '理货员',
			index: 'createUserName',
			width: 80
		}
	];
	tallyDataList = [];
	constructor(
		private fb: FormBuilder,
		private cargoStorageService: CargoStorageService,
	) {
	}

	ngOnInit(): void {
		this.loadStorageData();
	}

	loadStorageData(): any {
		this.storagePageInfo.loading = true;
		const params = {
			preAreaId: this.storageInfo?.preAreaId,
			positionId: this.storageInfo.positionId,
			cargoType: this.storageInfo.cargoType,
			packageType: this.storageInfo.packageType,
			...this.storagePageInfo,
		};
		this.cargoStorageService.storageByPositionDetailPage(params).subscribe((res: any) => {
			this.storageDataList = res.records;
			this.storagePageInfo.total = res.total;
			this.storagePageSetting.total = res.total;
			this.storagePageInfo.loading = false;

			if (this.storageDataList.length > 0) {
				this.selectedStorage = this.storageDataList[0];
				this.tallyPageInfo.current = 1;
				this.loadTallyData();
			}
		});
	}

	loadTallyData(): any {
		this.tallyPageInfo.loading = true;
		const params = {
			id: this.selectedStorage.id,
			...this.tallyPageInfo,
		};
		this.cargoStorageService.storageByPositionTallyPage(params).subscribe((res: any) => {
			this.tallyDataList = res.records;
			this.tallyPageInfo.total = res.total;
			this.tallyPageSetting.total = res.total;
			this.tallyPageInfo.loading = false;
		});
	}


	storageStChange(ret: STChange) {
		if (ret.type === 'click') {
			if (ret.click.item) {
				this.selectedStorage = deepCopy(ret.click.item);
				this.tallyPageInfo.current = 1;
				this.loadTallyData();
			}
		}
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.storagePageInfo.current = ret.pi;
			this.storagePageInfo.size = ret.ps;
			this.loadStorageData();
		}
	}

	tallyStChange(ret: STChange) {
		if (ret.type === 'pi' || ret.type === 'ps') {
			this.tallyPageInfo.current = ret.pi;
			this.tallyPageInfo.size = ret.ps;
			this.loadTallyData();
		}
	}

	sa: STClickRowClassNameType = {
		fn(record: any, index: number): string {
			return 'selected-row';
		},
		exclusive: true
	};

}
